<?php

namespace App\Http\Controllers\Admin\Manage;

use App\B2BProject\Admin\AdminControllerBuildTrait;
use App\B2BProject\Custom\LogisticsServer;
use App\B2BProject\Custom\MemberInformation;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LogisticsPriceController extends Controller
{
    use AdminControllerBuildTrait;
    protected $moduleName = '';

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $request->query->set('page', $request->query('page', $request->page));
            $request->query->set('pageSize', $request->query('pageSize', $request->pageSize));

            $query = LogisticsServer::orderBy('created_at','desc');

            if ($company = $request->query('company')) { //公司名称
                $query->whereHas('member.information', function($query) use ($company) {
                    $query->where('company', 'like', "%$company%");
                });
            }
            if ($tel = $request->query('tel')) { //电话
                $query->where('phone', $tel);
            }
            if ($request->query('state')) {
                if ($request->query('state') == 1) { //状态
                    $query->where('status', 1);
                }
                if ($request->query('state') == 2) {
                    $query->where('status', 0);
                }
            }

            $logistics = $query->paginate($request->query('pageSize', 7));
            $data = [];
            foreach ($logistics as $logistic) {
                $item = [
                    'id'           => $logistic->id,
                    'company'      => $logistic->member->information->company,
                    'load_point'   => $logistic->load_position,
                    'unload_point' => $logistic->unload_position,
                    'goods_name'   => $logistic->name,
                    'amount'       => $logistic->amonut,
                    'tel'          => $logistic->phone,
                    'operate'      => $logistic->status,
                    //0 未处理  1 处理
                    'reply_url'    => route('admin.logistics.enquiry.update',
                        [$logistic->id, 'page' => $request->input('page')]),
                    //更新

                    'detail_url'   => route('admin.logistics.enquiry.edit', [$logistic->id, 'page' => $request->input('page')]),
                    //编辑页面
                ];
                if ($logistic->status) {
                    $item['state'] = '已处理';
                } else {
                    $item['state'] = '未处理';
                }
                $data[] = $item;
            }
            return response()->json([
                'data'           => $data,
                'pagingConfData' => [
                    'page'     => $logistics->currentPage(),
                    'pageSize' => $request->query('pageSize'),
                    'total'    => $logistics->total(),
                ],
            ]);
        }
        return view('admin/manage/index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $ls = LogisticsServer::findOrNew($id);
        return view('admin/manage/edit')->with('lsinfo', $ls);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //处理
        $data = [
            'reply'  => $request->input('reply_text'),
            'status' => 1
        ];// 只取这些数据
        $ls   = LogisticsServer::findOrFail($id);
        $ls->update($data);

        return $this->index($request);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request, $id)
    {

    }
}
